package ca.uwindsor.cs.deepweb.utility;

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;

import ca.uwindsor.cs.deepweb.estimation.FilesContentIndexer;

public class SimpleIndex {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length !=1 ) {
			System.exit(0);
		}
		try {
			IndexWriter writer = new IndexWriter(args[0], new StandardAnalyzer(), true);
			Document document1 = new Document();
			Field FieldPath = new Field(FilesContentIndexer.FIELD_ID,
					"Document1", Field.Store.YES, Field.Index.UN_TOKENIZED);

			Field FieldBody = new Field(
					FilesContentIndexer.FIELD_CONTENT, "Query1 Query3 Query4",
					Field.Store.NO, Field.Index.TOKENIZED,
					Field.TermVector.YES);

			document1.add(FieldPath);
			document1.add(FieldBody);
			writer.addDocument(document1);
			
			Document document2 = new Document();
			FieldPath = new Field(FilesContentIndexer.FIELD_ID,
					"Document2", Field.Store.YES, Field.Index.UN_TOKENIZED);

			FieldBody = new Field(
					FilesContentIndexer.FIELD_CONTENT, "Query1 Query2 Query4",
					Field.Store.NO, Field.Index.TOKENIZED,
					Field.TermVector.YES);

			document2.add(FieldPath);
			document2.add(FieldBody);
			writer.addDocument(document2);
			
			Document document3 = new Document();
			 FieldPath = new Field(FilesContentIndexer.FIELD_ID,
					"Document3", Field.Store.YES, Field.Index.UN_TOKENIZED);

			 FieldBody = new Field(
					FilesContentIndexer.FIELD_CONTENT, "Query1 Query2 Query3",
					Field.Store.NO, Field.Index.TOKENIZED,
					Field.TermVector.YES);

			document3.add(FieldPath);
			document3.add(FieldBody);
			writer.addDocument(document3);
			
			Document document4 = new Document();
			 FieldPath = new Field(FilesContentIndexer.FIELD_ID,
					"Document4", Field.Store.YES, Field.Index.UN_TOKENIZED);

			 FieldBody = new Field(
					FilesContentIndexer.FIELD_CONTENT, "Query3",
					Field.Store.NO, Field.Index.TOKENIZED,
					Field.TermVector.YES);

			document4.add(FieldPath);
			document4.add(FieldBody);
			writer.addDocument(document4);
			
			Document document5 = new Document();
			FieldPath = new Field(FilesContentIndexer.FIELD_ID,
					"Document5", Field.Store.YES, Field.Index.UN_TOKENIZED);

			FieldBody = new Field(
					FilesContentIndexer.FIELD_CONTENT, "Query1",
					Field.Store.NO, Field.Index.TOKENIZED,
					Field.TermVector.YES);

			document5.add(FieldPath);
			document5.add(FieldBody);
			writer.addDocument(document5);
			writer.optimize();
			writer.close();
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
